package com.quik.web.user.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.quik.entity.SysAccount;
import com.quik.entity.SysRoleRight;
import com.quik.web.base.mapper.PubSystemDao;
import com.quik.web.user.mapper.AccountDao;
import com.quik.common.Constants;

/**
 * 功能说明：
 * 		实现帐户业务逻辑接口AccountService
 * 
 * @author jenny
 *
 */
@Service
public class AccountServiceImpl implements AccountService {
	
	@Autowired
	private AccountDao dao;
	
	@Autowired
	private PubSystemDao pubDao;
	
	/**
	 * 登录
	 * @param loginName
	 * @param password
	 * @return 如果登录成功，返回SysAccount对象，否则返回NULL
	 * @throws Exception
	 */
	@Override
	public SysAccount login(String loginName, String password) throws Exception{
		if(loginName == null || loginName.trim().length() <= 0)
			return null;
		
		if(password == null || password.trim().length() <= 0)
			return null;
		
		SysAccount account = dao.getAccountByStaffNoOrLoginName(loginName.trim());
		
		if(account != null && account.getLogin_password().equals(password.trim()))
			return account;
		else
			return null;
		
	}
	
	/**
	 * 查询用户类型变量列表
	 * @return
	 * @throws Exception
	 */
	@Override
	public List findUserTypeCode() throws Exception{
		StringBuilder section = new StringBuilder("[");
		section.append(SysAccount.TABLE_NAME);
		section.append("]_");
		section.append(Constants.SYSTEM_CODE_USERTYPE);
		
		List list = pubDao.findSystemCodeBySection(section.toString());
		
		return list;
	}

	/**
	 * 新增用户
	 * @param sysAccount
	 * @throws Exception
	 */
	@Override
	public void addUser(SysAccount sysAccount) throws Exception{
		dao.addUser(sysAccount);
	}
	
	/**
	 * 检查工号是否重复  
	 * @param staffNo 
	 * @return 如果存在返回true, 否则else
	 * @throws Exception
	 */
	@Override
	public boolean checkStaffNo(String staffNo) throws Exception{
		int count = dao.findAccountCountByStaffNo(staffNo);
		
		if(count > 0)
			return true;
		else
			return false;
	}
	
	/**
	 * 检查登录名是否重复
	 * @param loginName
	 * @return 如果存在返回true, 否则else
	 * @throws Exception
	 */
	@Override
	public boolean checkLoginName(String loginName) throws Exception{
		int count = dao.findAccountCountByLoginName(loginName);
		
		if(count > 0)
			return true;
		else
			return false;
	}
	
	/**
	 * 根据查询条件查询用户总数
	 * @param staffNo
	 * @param loginName
	 * @return
	 * @throws Exception
	 */
	@Override
	public int findUserCount(Integer userType, String staffNo, String loginName) throws Exception{
		int count = dao.findUserCount(userType, staffNo, loginName);
		return count;
	}
	
	/**
	 * 根据查询条件查询用户（分页）
	 * @param staffNo
	 * @param loginName
	 * @param page
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<SysAccount> findUser(Integer userType, String staffNo, String loginName, Integer page, Integer pageSize) throws Exception{
		List<SysAccount> list = dao.findUser(userType, staffNo, loginName, page, pageSize);
		
		return list;
	}
	
	/**
	 * 查询用户班次变量列表
	 * @return
	 * @throws Exception
	 */
	@Override
	public List findClassesCode() throws Exception{		
		List list = pubDao.findSystemCodeBySection(Constants.SYSTEM_CODE_CLASSES);
		
		return list;
	}

	@Override
	public List<SysRoleRight> findRoleRightByUser(SysAccount user) {
		return this.dao.findRoleRightByUser(user);
	}
}
